/**
 * Created by code_Xiao on 2018/08/23.
 */
import axios from 'axios';
import { Message, Loading} from 'element-ui'
import md5 from 'js-md5'

// 创建axios实例
const service = axios.create({
    headers: {
        'token': '',
        "version": "1",
        "times": Date.parse(new Date()),
        "sign": md5(Date.parse(new Date()) + "fdasfasv72c92a549dd5a330112"),
        "client-type": "2"
    },
    timeout: 50000                  // 请求超时时间
});

// 请求拦截
var loadinginstace;
service.interceptors.request.use(
    config => {
        // 设置请求头
        config.headers.times = Date.parse(new Date());
        config.headers.sign = md5(Date.parse(new Date()) + "fdasfasv72c92a549dd5a330112");

        // element ui Loading方法
        loadinginstace = Loading.service({
            fullscreen: true,
            text: '加载中...'
        });
        return config
    },
    err => {
        loadinginstace.close();
        Message.error('加载超时');
        return Promise.reject(err)

    }
);
service.interceptors.response.use(
    response => {
        loadinginstace.close();
        return Promise.resolve(response);
    },
    error => {
        loadinginstace.close();
        Message.error('请求失败');
        return Promise.reject(error)
    }
)
export default service;
